const app = new getApp();
var time = require("../../../../utils/util.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    prompt: '',
    default_img: {},
    status: 'all',
    page: 1,
    order_list: [],
    refundFlag: 0,
    operationFlag: 0,
    cancleRefundFlag: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;

    // 订单状态
    let status = 'all';
    if (options.status){
      status = options.status;
    }
    // 订单类型
    let order_type = 'all';
    if (options.order_type) {
      order_type = options.order_type;
    }

    that.setData({
      status: status,
      order_type: order_type
    })

    app.sendRequest({
      url: 'System.Order.orderStatus',
      data: {
        order_type: order_type,
      },
      success: function (res) {
        let code = res.code;
        let data = res.data;

        if (code == 0) {
          let order_status_list = app.checkArray(data) ? data : [];
          that.setData({
            order_status_list: order_status_list
          })
        }
      }
    });
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.restStatus(this, 'operationFlag');
    app.defaultImg(this, true);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this;
    let page = that.data.page;
    let status = that.data.status;
    let order_type = that.data.order_type;
    let order_list = that.data.order_list;

    app.sendRequest({
      url: 'System.Order.order',
      data: {
        page: page,
        shop_id: 0,
        status: status,
        order_type: order_type,
      },
      success: function (res) {
        let code = res.code;
        if (code == 0) {
          let new_order_list = res.data.data;
          let d = {};
          page = new_order_list.length > 0 ? page+1 : page;

          for (let index in new_order_list) {
            new_order_list[index].create_time = time.formatTime(new_order_list[index].create_time, 'Y-M-D h:m:s');
            new_order_list[index].operation = {}; //去除无用数组
            //图片处理
            for (let key in new_order_list[index].order_item_list) {
              let img = new_order_list[index].order_item_list[key].picture.pic_cover_small;
              new_order_list[index].order_item_list[key].picture.pic_cover_small = app.IMG(img);
            }
            //优化数据传入
            let key = "order_list[" + (parseInt(order_list.length) + parseInt(index)) + "]";
            d[key] = new_order_list[index];
          }
          //更新加入数据
          that.setData(d);

          that.setData({
            page: page
          })
        }
      }
    });
  },

  /**
   * 加载订单信息
   */
  loadInfo: function () {
    let that = this;
    let status = that.data.status;
    let order_type = that.data.order_type;

    app.sendRequest({
      url: 'System.Order.order',
      data: {
        page: 1,
        shop_id: 0,
        status: status,
        order_type: order_type,
      },
      success: function (res) {
        let code = res.code;

        if (code == 0) {
          let order_list = res.data.data;

          for (let index in order_list) {
            order_list[index].create_time = time.formatTime(order_list[index].create_time, 'Y-M-D h:m:s');
            //图片处理
            for (let key in order_list[index].order_item_list) {
              let img = order_list[index].order_item_list[key].picture.pic_cover_small;
              order_list[index].order_item_list[key].picture.pic_cover_small = app.IMG(img);
            }
          }
          let page = order_list.length > 0 ? 2 : 1;
          that.setData({
            order_list: order_list,
            page: page
          })
        }
      }
    })
  },

  /**
   * 图片加载失败
   */
  errorImg: function (e) {
    let that = this;
    let index = e.currentTarget.dataset.index;
    let key = e.currentTarget.dataset.key;
    let order_list = that.data.order_list;
    let default_img = that.data.default_img;
    let parm = {};
    let img = order_list[index].order_item_list[key].picture.pic_cover_small;

    if (default_img.is_use == 1) {
      default_img = default_img.value.default_goods_img;
      if (img.indexOf(default_img) == -1) {
        let parm_key = "order_list[" + index + "].order_item_list[" + key + "].picture.pic_cover_small";

        parm[parm_key] = default_img;
        that.setData(parm);
      }
    }
  },

  /**
   * 顶部导航选中
   */
  selectOrderStatus: function (event) {
    let that = this;
    let status = event.currentTarget.dataset.status;
    let order_type = that.data.order_type;

    that.setData({
      status: status,
    })
    app.sendRequest({
      url: 'System.Order.order',
      data: {
        page: 1,
        shop_id: 0,
        status: status,
        order_type: order_type,
      },
      success: function (res) {
        let code = res.code;
        if (code == 0) {
          let order_list = res.data.data;
          for (let index in order_list) {
            order_list[index].create_time = time.formatTime(order_list[index].create_time, 'Y-M-D h:m:s')
            //图片处理
            for (let key in order_list[index].order_item_list){
              let img = order_list[index].order_item_list[key].picture.pic_cover_small;
              order_list[index].order_item_list[key].picture.pic_cover_small = app.IMG(img);
            }
          }

          that.setData({
            order_list: order_list,
            page: 2
          })
        }
      }
    })
  },

  /**
   * 随便逛逛
   */
  aIndex: function(event){
    let url = event.currentTarget.dataset.url;
    wx.switchTab({
      url: url,
    })
  },
  
  /**
   * 订单操作
   */
  operation: function(event){
    let that = this;
    let operationFlag = that.data.operationFlag;
    let action = event.currentTarget.dataset.action;
    let order_id = event.currentTarget.dataset.id;

    if (operationFlag == 1){
      return false;
    }
    app.clicked(that, 'operationFlag');

    switch (action) {
      case 'pay': //支付
        that.pay(order_id);
        break;
      case 'close': //订单关闭
        that.orderClose(order_id);
        break;
      case 'getdelivery': //订单收货
        that.getDelivery(order_id);
        break;
      case 'logistics': //查看物流
        that.logistics(order_id);
        break;
      case 'delete_order': //删除订单
        that.deleteOrder(order_id);
        break;
      case 'pay_presell': //预定金支付
        that.payPresell(order_id);
        break;
      case 'member_pickup': // 买家自提
        that.memberPickup(order_id);
        break;
      default:
        app.restStatus(that, 'operationFlag');
        break;
    }
  },
  
  /**
   * 去支付
   */
  pay: function(order_id){
    let that = this;

    app.sendRequest({
      url: 'System.Pay.orderPay',
      data: {
        order_id: order_id,
      },
      success: function (res) {
        let code = res.code;
        let data = res.data;
        if (code == 0) {
          if (data != undefined && (typeof(data) == 'int' || typeof(data) == 'string')) {
            let out_trade_no = data;
            let url = '/pagesother/pages/pay/getpayvalue/getpayvalue?out_trade_no=' + out_trade_no;
            wx.navigateTo({
              url: url,
            })
          } else {
            app.showBox(that, '支付失败');
            app.restStatus(that, 'operationFlag');
          }
        } else {
          app.showBox(that, res.message);
          app.restStatus(that, 'operationFlag');
        }
      }
    });
  },

  /**
   * 关闭订单
   */
  orderClose: function(order_id){
    let that = this;

    app.sendRequest({
      url: 'System.Order.orderClose',
      data: {
        order_id: order_id
      },
      success: function (res) {
        let code = res.data;
        if (code > 0){
          app.showBox(that, '关闭成功');
          that.loadInfo();
        } else {
          app.showBox(that, '关闭失败');
          app.restStatus(that, 'operationFlag');
        }
      }
    })
  },

  /**
   * 确认收货
   */
  getDelivery: function (order_id) {
    let that = this;

    app.sendRequest({
      url: 'System.Order.orderTakeDelivery',
      data: {
        order_id: order_id,
      },
      success: function (res) {
        let code = res.data;
        if (code > 0) {
          app.showBox(that, '收货成功');
          that.loadInfo();
        } else {
          app.showBox(that, '收货失败');
          app.restStatus(that, 'operationFlag');
        }
      }
    });
  },

  /**
   * 查看物流
   */
  logistics: function (order_id) {
    let that = this;

    wx.navigateTo({
      url: '/pagesother/pages/order/logistics/logistics?order_id=' + order_id,
    })
  },

  /**
   * 删除订单
   */
  deleteOrder: function (order_id){
    let that = this;

    app.sendRequest({
      url: 'System.Order.deleteOrder',
      data: {
        order_id: order_id
      },
      success: function (res) {
        let code = res.data;
        if (code > 0) {
          app.showBox(that, '订单删除成功');
          that.loadInfo();
        }else{
          app.showBox(that, '订单删除失败');
          app.restStatus(that, 'operationFlag');
        }
      }
    });
  },

  /**
   * 定金支付
   */
  payPresell: function (order_id) {
    let that = this;

    app.sendRequest({
      url: 'System.Pay.orderPresellPay',
      data: {
        order_id: order_id,
      },
      success: function (res) {
        let code = res.code;
        let data = res.data;
        if (code == 0) {
          if (data != undefined && (typeof (data) == 'int' || typeof (data) == 'string')) {
            let out_trade_no = data;
            let url = '/pagesother/pages/pay/getpayvalue/getpayvalue?out_trade_no=' + out_trade_no;
            wx.navigateTo({
              url: url,
            })
          } else {
            app.restStatus(that, 'operationFlag');
            app.showBox(that, '支付失败');
          }
        } else {
          app.showBox(that, res.message);
          app.restStatus(that, 'operationFlag');
        }
      }
    });
  },

  /**
   * 买家自提
   */
  memberPickup: function (order_id) {
    wx.navigateTo({
      url: '/pagesother/pages/order/pickup/pickup?order_id=' + order_id,
    })
  },
  
  /**
   * 取消退款
   */
  cancleRefund: function (e) {
    let that = this;
    let order_id = e.currentTarget.dataset.order_id;
    let order_goods_id = e.currentTarget.dataset.order_goods_id;
    let cancleRefundFlag = that.data.cancleRefundFlag;
    if (cancleRefundFlag == 1) {
      return false;
    }
    app.clicked(that, 'cancleRefundFlag');
    app.sendRequest({
      url: 'System.Order.cancelOrderRefund',
      data: {
        order_id: order_id,
        order_goods_id: order_goods_id
      },
      success: function (res) {
        if (res.code == 0) {
          if (res.data > 0) {
            app.restStatus(that, 'cancleRefundFlag');
            app.showBox(that, '退款取消成功');
            that.loadInfo();
          } else {
            app.restStatus(that, 'cancleRefundFlag');
            app.showBox(that, '退款取消失败');
          }
        }
      }
    });
  },
})